package com.iflytek.uoamp.agent.common.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.log4j.Logger;

public class Sign {

	private static final int MD5LEN = 16;
	private Sign() {

	}



	/**
	 * String to byte
	 */
	private static byte stringHEX(String str) {
		String str1 = noarmal(str.substring(0, 1));
		String str2 = noarmal(str.substring(1));
		int h = Integer.parseInt(str1);
		int l = Integer.parseInt(str2);
		h = h << 4;
		if ((h & 0x80) != 0) {
			h = h & 0x7F;
			int result = (h | l) + 1;
			result = -result;
			return (byte) (result & 0xff);
		}
		return (byte) (h | l);
	}

	/**
	 * 16进制 to 10进制
	 */
	private static String noarmal(String str) {
		if (("a").equalsIgnoreCase(str)) {
			return "10";
		} else if (("b").equalsIgnoreCase(str)) {
			return "11";
		} else if (("c").equalsIgnoreCase(str)) {
			return "12";
		} else if (("d").equalsIgnoreCase(str)) {
			return "13";
		} else if (("e").equalsIgnoreCase(str)) {
			return "14";
		} else if (("f").equalsIgnoreCase(str)) {
			return "15";
		}
		return str;
	}
	
	/**
	 * byte to String
	 */
	private static String byteHEX(int ib) {
		byte[] digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
		byte[] ob = new byte[2];
		int h = 0;
		int l = 0;

		if (ib < 0) {

			ib = -ib;
			ib = ib - 1;

			h = (ib >> 4) & 0x0f;
			l = ib & 0x0f;
			h = h | 0x8;
		} else {
			h = (ib >> 4) & 0x0f;
			l = ib & 0x0f;
		}

		ob[0] = digit[h];
		ob[1] = digit[l];
		String s = new String(ob);
		return s;
	}


	/**
	 * 获取MD5签名.
	 */
	public static byte[] getSign(String text) {
		if (text == null) {
			return new byte[] { -44, 29, -116, -39, -113, 0, -78, 4, -23, -128, 9, -104, -20, -8, 66, 126 };
		}
		byte[] sign = { -44, 29, -116, -39, -113, 0, -78, 4, -23, -128, 9, -104, -20, -8, 66, 126 };
		try {
			sign = text.getBytes("utf-8");
		} catch (Exception e1) {
			Logger.getRootLogger().error(e1.getMessage(),e1);
		}
		return getSign(sign);
	}



	/**
	 * @param sign
	 * @return
	 */
	public static byte[] getSign(byte[] sign) {
		try {
			MessageDigest digester = MessageDigest.getInstance("MD5");
			digester.update(sign);
			sign = digester.digest();
			if (sign.length == MD5LEN) {
				return sign;
			}
		} catch (NoSuchAlgorithmException e) {
			Logger.getRootLogger().error(e.getMessage(),e);
		}
		return new byte[] {};
	}

	public static boolean compare(byte[] s1, byte[] s2) {
		if (s1.length != s2.length) {
			return false;
		}
		for (int i = 0; i < s1.length; i++) {
			if (s1[i] != s2[i]) {
				return false;
			}
		}
		return true;
	}

	public static boolean compare(char[] s1, char[] s2) {
		if (s1.length != s2.length) {
			return false;
		}
		for (int i = 0; i < s1.length; i++) {
			if (s1[i] != s2[i]) {
				return false;
			}
		}
		return true;
	}

	public static boolean compare(String o1, String o2) {
		if (o1.equalsIgnoreCase(o2)) {
			return true;
		}
		return false;
	}

	/**
	 * byte[] to String
	 */
	public static String byteToString(byte[] sign) {
		StringBuilder digestHexStr = new StringBuilder("");
		for (int i = 0; i < sign.length; i++) {
			digestHexStr.append(byteHEX(sign[i]));
		}
		return digestHexStr.toString();
	}

	

	/**
	 * String to bytes
	 */
	public static byte[] stringToByte(String sign) {
		if (32 != sign.length()) {
			return null;
		}
		byte[] result = new byte[16];
		for (int i = 0; i < sign.length(); i = i + 2) {
			result[i / 2] = stringHEX(sign.substring(i, i + 2));
		}
		return result;
	}
}
